package org.poem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.poem.id.SnowFlake;
import org.poem.datasource.DataSourceConfigVo;
import org.poem.entity.TDatasourceConfig;
import org.poem.mapper.TDataSourceConfigMapper;
import org.poem.service.ITDataSourceConfigService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据源参数 服务实现类
 * </p>
 *
 * @author vinshine
 * @since 2019-10-24
 */
@Service
public class TDataSourceConfigServiceImpl extends ServiceImpl<TDataSourceConfigMapper, TDatasourceConfig> implements ITDataSourceConfigService {

    /**
     * 01-根据数据源id查询所有参数
     * @param dataSourceId    数据源id
     * @return
     */
    @Override
    public List<DataSourceConfigVo> findTDataSourceParametersList(Long dataSourceId) {
        QueryWrapper<TDatasourceConfig> ew = new QueryWrapper<>();
        ew.lambda().eq(TDatasourceConfig::getDataSourceId, dataSourceId);
        List<TDatasourceConfig> list = this.list(ew);
        List<DataSourceConfigVo> dataSourceConfigVos = new ArrayList<>();
        for (TDatasourceConfig o : list) {
            DataSourceConfigVo parametersVo = new DataSourceConfigVo();
            parametersVo.setDataSourceId(o.getDataSourceId());
            parametersVo.setId(o.getId());
            parametersVo.setName(o.getName());
            parametersVo.setStatus(o.getStatus());
            parametersVo.setValue(o.getValue());
            dataSourceConfigVos.add(parametersVo);
        }
        return dataSourceConfigVos;
    }

    /**
     * 02-添加数据源参数
     * @param dataSourceConfigVo    数据源参数实体类
     * @return
     */
    @Override
    public String addOrUpdateDataSourceParameters(List<DataSourceConfigVo> dataSourceConfigVo,   Long dataSourceId) {
        if(dataSourceConfigVo.size()<=0){
            return "没有数据";
        }
        //过滤掉用户没有没有输入无用参数
        for (int i = 0; i < dataSourceConfigVo.size(); i++) {
            DataSourceConfigVo sourceParametersVo = dataSourceConfigVo.get(i);
            if("".equals(sourceParametersVo.getName()) && "".equals(sourceParametersVo.getName())){
                dataSourceConfigVo.remove(i);
            }
        }
        if(dataSourceConfigVo.size()<=0){
            return "没有数据";
        }
        //新增数据源或修改数据源时会根据id先删除该数据源的所有参数
        QueryWrapper<TDatasourceConfig> ew = new QueryWrapper<>();

        ew.lambda().eq(TDatasourceConfig::getDataSourceId, dataSourceId);

        ArrayList<TDatasourceConfig> lists = new ArrayList<>();
        for (DataSourceConfigVo sourceParametersVo : dataSourceConfigVo) {
            TDatasourceConfig tDataSourceConfig = new TDatasourceConfig();

            tDataSourceConfig.setCreateTime(LocalDateTime.now());
            tDataSourceConfig.setDataSourceId(dataSourceId);
            tDataSourceConfig.setId(SnowFlake.genLongId());
            tDataSourceConfig.setName(sourceParametersVo.getName());
            tDataSourceConfig.setStatus(sourceParametersVo.getStatus());
            tDataSourceConfig.setUpdateTime(LocalDateTime.now());
            tDataSourceConfig.setUpdateUser(sourceParametersVo.getUpdateUser());
            tDataSourceConfig.setValue(sourceParametersVo.getValue());
            lists.add(tDataSourceConfig);
        }
        boolean b = this.saveOrUpdateBatch(lists);
        if(b){
            return "成功！";
        }
        return "失败！";
    }

    /**
     * 03-根据数据源id查询对应的参数
     * @param sourceId
     * @return
     */
    @Override
    public List<DataSourceConfigVo> getDataSourceParametersVo(Long sourceId) {
        QueryWrapper<TDatasourceConfig> ew = new QueryWrapper<>();
        ew.lambda().eq(TDatasourceConfig::getDataSourceId, sourceId);
        List<TDatasourceConfig> list = this.list(ew);
        List<DataSourceConfigVo> data = new ArrayList<>();
        for (TDatasourceConfig tDataSourceConfig : list) {
            DataSourceConfigVo parameters = new DataSourceConfigVo();
            parameters.setId(tDataSourceConfig.getId());
            parameters.setName(tDataSourceConfig.getName());
            parameters.setValue(tDataSourceConfig.getValue());
            parameters.setDataSourceId(tDataSourceConfig.getDataSourceId());
            data.add(parameters);
        }
        return data;
    }

    /**
     * 04-根据数据源id删除数据参数
     * @param sourceId
     * @return
     */
    @Override
    public String deleteDataSourceParametersById(Long sourceId) {
        QueryWrapper<TDatasourceConfig> ew = new QueryWrapper<>();
        ew.lambda().eq(TDatasourceConfig::getDataSourceId, sourceId);
        boolean b = this.remove(ew);
        if(b){
            return "删除成功！";
        }
        return "删除失败！";
    }
}
